package org.aom.litebean.module.menu;

import org.aom.entity.menu.Module;
import org.aom.litebean.module.base.BaseGridCrudBean;
import org.aom.service.menu.IModuleManager;
import org.aom.util.ManageBeanUtil;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.component.grid.provider.GridDataProvider;

/**
 * 主模块管理页面的托管Bean
 * 
 * @author Saya
 */
@ManagedBean(name = "module.menu.moduleListBean", scope = ManagedBeanScope.REQUEST)
public class ModuleListBean extends BaseGridCrudBean<Module, Long> {

	@ManagedProperty("#{moduleManager}")
	private IModuleManager moduleManager;

	@Bind(id = "moduleDataGrid", attribute = "binding")
	private UIDataGrid moduleDataGrid;

	@Bind(id = "moduleDataGrid", attribute = "idColumn")
	private static String idColumn = "id";

	@Bind(id = "moduleDataGrid", attribute = "dataProvider")
	public GridDataProvider getDataProvider() {
		return super.getDataProvider();
	}

	@Override
	protected Class<Module> getEntityClass() {
		return Module.class;
	}

	@Override
	protected IModuleManager getEntityManager() {
		return this.moduleManager;
	}

	@Override
	protected String getIdColumn() {
		return idColumn;
	}

	@BeforeRender
	public void beforeRender(boolean isPostBack) {
		initBaseResource();
	}

	@Override
	@Action(id = "add", event = "onclick")
	protected void processToAdd() {
		String url = "moduleItemManage.faces";
		ManageBeanUtil.openDialog(null, "add", OperationFrame, url,
				OperationDialog, 500, 400);
	}

	@Override
	@Action(id = "moduleDataGrid", event = "ondblclick")
	protected void processToEdit() {
		try {
			Module module = (Module) getSelectEntity();
			String url = "moduleItemManage.faces";
			ManageBeanUtil.openDialog(module.getId(), "mod", OperationFrame,
					url, OperationDialog, 500, 400);
		} catch (ArrayIndexOutOfBoundsException e) {
			ManageBeanUtil.noSelectWarn();
		}
	}

	@Override
	protected void processToSearch() {
	}

	@Override
	protected void initAdd() {
	}

	@Override
	protected UIDataGrid getDataGrid() {
		return moduleDataGrid;
	}

	@Override
	protected void delete() {
		try {
			Module entity = (Module) getSelectEntity();
			moduleManager.delete(entity.getId());
			resetGrid();
		} catch (ArrayIndexOutOfBoundsException e) {
			ManageBeanUtil.noSelectWarn();
		}

	}
}
